<?php

declare(strict_types=1);

namespace app\index\controller;

use app\common\model\Mutual;
use app\common\model\Account;
use app\common\model\Publicity;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Help extends Base
{
    /**
     *保障首页
     */
    public function index()
    {
        return View::fetch();
    }
    /**
     * 申请互助
     */
    public function info()
    {
        $info = Mutual::where(['uid' => $this->user->id])->find();
        View::assign('info', $info);
        return View::fetch();
    }
    /**
     *提交申请
     */
    public function save()
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Mutual');
            return Result::fastReturn(Mutual::add($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 保障中
     */
    public function list()
    {
        return View::fetch();
    }
    /**
     * 获取记录
     */
    public function getList()
    {
        $param = $this->request->get();
        $map = ['status' => 2];
        $list = Mutual::getPageList($map, 'id,uid,rname,sex,idcard,create_time', $this->getPage(), 20, false, ['user']);
        foreach ($list['data'] as &$v) {
            $v->idcard = substr_replace($v->idcard, '**************', -14, 10);
            $v->rname = getHideRealName($v->rname);
            $v->sex_name = match ($v->sex) {
                1 => '男',
                2 => '女',
                default => '未知'
            };
        }
        return Result::fastReturn($list);
    }
    /**
     * 我的分摊
     */
    public function myaccount()
    {
        return View::fetch();
    }
    /**
     * 获取分摊记录
     */
    public function getAccountList()
    {
        $map = ['type' => 'help_money', 'uid' => $this->user->id, 'intro' => '互助基金分摊'];
        return Result::fastReturn(Account::getPageList($map, '*', $this->getPage(), 20, false, ['take']));
    }
    /**
     * 关爱公示
     */
    public function help_index()
    {
        return View::fetch();
    }
    /**
     *公示记录
     */
    public function getHelpList()
    {
        $map = ['status' => 1];
        $list = Publicity::getPageList($map, '*', $this->getPage(), 20, false);
        return Result::fastReturn($list);
    }
    /**
     * 公示详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $info = Publicity::find($id);
        View::assign('info', $info);
        return View::fetch();
    }
}
